import base from './baseapi';
import wepy from 'wepy'
import wxUtils from '../utils/wxUtils'

/**
 * 权限服务类
 */

export default class auth extends base {
  // 登录检查
  static async tryLogin() {
    // 如果登录就跳过
    let openId = wepy.$instance.globalData.jwtToken
    if (!openId) {
      wxUtils.redirectToPage('/pkgvshop/home/login', true)
    } else {
      // 获取用户信息
      let userInfo = await wx.cloud.callFunction({
        name: 'getUserInfoByOPENID',
        data: {
          _openid: openId
        }
      })
      // 判断数据库里面是否有
      if (userInfo.result.code === '0') {
        wepy.$instance.globalData.userInfo = userInfo.result.data.userInfo  // 赋值用户信息
      } else {
        wxUtils.redirectToPage('/pkgvshop/home/login', true)
      }
    }
    wepy.$instance.globalData.isInit = true
  }
}
